package org.yn.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.yn.user.domain.User;
import org.yn.user.dto.BindSendSmsCodeDTO;
import org.yn.user.dto.RegisterDTO;
import org.yn.user.query.UserQuery;
import org.yn.user.service.IUserService;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@PermissionAnnotation(name="user管理",descs = "user管理")
@Api("user管理接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public IUserService userService;


    /**
     * 保存和修改公用的
     * @param user 传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name = "保存和修改User", descs = "保存和修改User")
    @ApiOperation(value = "保存和修改")
    @PostMapping
    public AjaxResult updateOrInsert(@RequestBody User user) {
        if ( user.getId() != null)
            userService.update(user);
            else
            userService.insert(user);
        return AjaxResult.success();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PermissionAnnotation(name = "删除User", descs = "删除User")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
            userService.delete(id);
        return AjaxResult.success();
    }

    //获取用户
    @PermissionAnnotation(name = "获取单个User", descs = "获取单个User")
    @ApiOperation(value = "获取单个User")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id) {
        User user =userService.selectById(id);
        return AjaxResult.success(user);
    }


    //获取用户
    @PermissionAnnotation(name = "根据电话号码获取单个User", descs = "根据电话号码获取单个User")
    @ApiOperation(value = "根据电话号码获取单个User")
    @GetMapping("/{phone}")
    public AjaxResult selectByPhone(@PathVariable("phone") String phone) {
        User user =userService.selectByPhone(phone);
        return AjaxResult.success(user);
    }


    /**
    * 查看所有的XX信息
    * @return
    */
    @PermissionAnnotation(name = "获取所有User", descs = "获取所有User")
    @ApiOperation(value = "获取所有User")
    @GetMapping
    public AjaxResult selectAll() {

        List< User> list = userService.selectAll();
        return AjaxResult.success(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageInfo 分页对象
    */
    @PermissionAnnotation(name = "分页查询User", descs = "分页查询User")
    @ApiOperation(value = "分页查询User")
    @PutMapping
    public AjaxResult queryPage(@RequestBody UserQuery query) {
        PageInfo<User> pageInfo = userService.queryPage(query);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PermissionAnnotation(name = "批量删除User", descs = "批量删除User")
    @ApiOperation(value = "批量删除User")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
            userService.batchDelete(ids);
        return AjaxResult.success();
    }


    /**
     * 注册
     */

    @PostMapping("/register")
    public AjaxResult register(@Valid @RequestBody RegisterDTO dto){

       userService.register(dto);

        return AjaxResult.success();
    }

    @PostMapping("/bind/wechat")
    public  AjaxResult wechatBind(@RequestBody BindSendSmsCodeDTO dto){
        Map<String, Object> map=   userService.wechatBind(dto);


        return AjaxResult.success(map);
    }


}